package larc.dao.impl;

import java.util.List;
import java.sql.Date;

import org.hibernate.Query;
import org.hibernate.Session;
import larc.dao.intr.CitaDao;
import larc.model.Cita;
import larc.util.HibernateUtil;

public class CitaDaoImpl implements CitaDao{

	public void insertarCita(Cita cita) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try{
		    session.beginTransaction();
		    session.m(cita);
		    session.getTransaction().commit();
		    session.close();
		    
			/*session.beginTransaction();
			session.save(cita);
			session.beginTransaction().commit();*/
		}catch(Exception e)
		{
			System.out.println("Error al insertar : " + e.getMessage());
			session.beginTransaction().rollback();
		}
		
	}

	public void actualizarCita(Cita cita) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try{
			session.beginTransaction();
			session.update(cita);
			session.beginTransaction().commit();
		}catch(Exception e)
		{
			System.out.println("Error al actualizar : " + e.getMessage());
			session.beginTransaction().rollback();
		}
		
	}

	public void eliminarCita(Cita cita) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try{
			session.beginTransaction();
			session.delete(cita);
			session.beginTransaction().commit();
		}catch(Exception e)
		{
			System.out.println("Error al eliminar : " + e.getMessage());
			session.beginTransaction().rollback();
		}
		
	}

	@SuppressWarnings("unchecked")
	public List<Cita> listarCitas() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		return session.createQuery("from Cita").list(); 
	}

	public Cita citaxId(Integer id) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "from Cita as c where c.ccCodigo=:id";
		Query query = session.createQuery(sql);
		query.setInteger("id",id);
		return (Cita) query.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> citasxFecha(Date sqlDate) {
		try{
			Session session = HibernateUtil.getSessionFactory().openSession();			
			
			//Query query = session.createQuery("select c from Cita c inner join c.detalleCitas d where d.dcFecha=:fecha");
			Query query = session.createQuery("SELECT " +
					"d.dcHora," +
					"d.dcEstado," +
					"t.tipoEvaNombre," +
					"d.dcSala," +
					"c.ccCv," +
					"c.empCodigo," +
					"e.empNombre," +
					"concat(p.persona.peApellidoP,' ',p.persona.peApellidoM,', ',p.persona.peNombres)," +
					"c.ccPuesto," +
					"em.emAbrev," +
					"c.emCodigo," +
					"c.conCodigo," +
					"n.persona.peNombres," +
					"d.dcObs " +
					"FROM " +
					"Cita c inner join c.detalleCitas d," +
					"TipoEvaluacion t," +
					"Empresa e," +
					"Empleados em," +
					"Postulante p, " +
					"Contactos n " +
					"WHERE " +
					"d.dcFecha=:fecha and " +
					"c.empCodigo = e.empCodigo and " +
					"c.emCodigo = em.emCodigo and " +
					"c.tipoEvaCodigo = t.tipoEvaCodigo and " +
					"c.conCodigo = n.conCodigo and " +
					"d.emCodigo = em.emCodigo and " +
					"c.PCodigo = p.PCodigo ORDER BY d.dcHora");
			
			query.setDate("fecha", sqlDate);		
			List<Object[]> lista = query.list();
			return lista;
		
		}catch(Exception e)
		{
			System.out.println("Error al realizar el query : " + e.getMessage());
		}
		return null;
	}
	
}
